import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.ImageIcon;

public class Player extends Entity
{
	int velocityX = 0;
	int velocityY = 0;
	int smallJump = 5;
	int highJump = 7;
	double normalGravity = 0.2;
	double gravity = normalGravity;
	double tempGravity = 0;
	int terminalVelocity = 4;
	int playerSpeed = 2;
	long startTime;
	long endTime;
	long timeDelay;
	
	int platformCollisionCode = 0;
	// Code 0 = Falling. Normal Gravity. Not Able to jump.
	// Code 1 = On top of platform. Gravity 0. Able to jump.
	// Code 2 = 
	
	public Player(int x, int y)
	{
		super(x, y);
	}

	public void update()
	{
		x += velocityX;
		y += velocityY;
		checkEnemyCollisions();
		gravity();
	}
	
	public void draw(Graphics2D g2)
	{
		g2.drawImage(getPlayerImg(), x, y, null);
		g2.draw(getBounds());
	}
	
	public Image getPlayerImg()
	{
		ImageIcon ic = new ImageIcon("Pictures/Megaman.png");
		return ic.getImage();
	}
	
	public void keyPressed(KeyEvent e)
	{
		int key = e.getKeyCode();		

		if ((key == KeyEvent.VK_LEFT) || (key == KeyEvent.VK_A)) 		// Move Left
		{
			velocityX = -playerSpeed;
		}
		else if ((key == KeyEvent.VK_RIGHT) || (key == KeyEvent.VK_D))	// Move Right
		{
			velocityX = playerSpeed;
		}
		if ((key == KeyEvent.VK_Z) || (key == KeyEvent.VK_J))			// Jump
		{
			startTime = System.currentTimeMillis();
		}
		if ((key == KeyEvent.VK_C) || (key == KeyEvent.VK_L))			// Shoot
		{
			Draw.addBullet(new Bullet(x + 15, y));
		}
	}
	
	public void keyReleased(KeyEvent e)
	{
		int key = e.getKeyCode();
		
		if ((key == KeyEvent.VK_LEFT) || (key == KeyEvent.VK_A))
		{
			velocityX = 0;
		}
		else if ((key == KeyEvent.VK_RIGHT) || (key == KeyEvent.VK_D))
		{
			velocityX = 0;
		}
		if ((key == KeyEvent.VK_Z) || (key == KeyEvent.VK_J))			// Jumpa
		{
			timeDelay = System.currentTimeMillis() - startTime;
			
			if (timeDelay < 150)
			{
				smallJump();
			}
			else
			{
				highJump();
			}
		}
	}
	
	public void smallJump()
	{
		checkPlatformCollisions();
		
		if (platformCollisionCode == 1)
		{
			velocityY = -smallJump;
			tempGravity = -smallJump;
			gravity = normalGravity;
		}
	}
	
	public void highJump()
	{
		checkPlatformCollisions();
		
		if (platformCollisionCode == 1)
		{
			velocityY = -highJump;
			tempGravity = -highJump;
			gravity = normalGravity;
		}
	}
	
	public void gravity()
	{
		checkPlatformCollisions();
		
		if (platformCollisionCode == 0)
		{
			if (velocityY < terminalVelocity)
			{
				tempGravity +=gravity;
				velocityY = (int)tempGravity;
			//	System.out.println("tempGravity" + tempGravity);
			//	System.out.println("velocityY" + velocityY);
			}
			else
			{
				velocityY = terminalVelocity;
			}
		}	
		else if (platformCollisionCode == 1)
		{
			gravity = 0;
			velocityY = 0;
		}
		
	}
	
	public void checkEnemyCollisions()
	{
		ArrayList<Enemy> enemies = Draw.getEnemyList();
		
		for (int i = 0; i < enemies.size(); i++)
		{
			Enemy tempEnemy = enemies.get(i);
			if (getBounds().intersects(tempEnemy.getBounds()))
			{
				//Draw.removeEnemy(tempEnemy);
				System.exit(0);
			}
		}		
	}
	
	public int checkPlatformCollisions()
	{
		ArrayList<Platform> platforms = Draw.getPlatformList();
		
		for (int i = 0; i < platforms.size(); i++)
		{
			Platform tempPlatform = platforms.get(i);
			if (getBounds().intersects(tempPlatform.getBounds()))
			{
				if (y < tempPlatform.getY())
				{
					platformCollisionCode = 1;
					return platformCollisionCode;
				}
			}
		}
		platformCollisionCode = 0;
		return platformCollisionCode;
	}
	
	public Rectangle getBounds()
	{
		return new Rectangle(x, y, getPlayerImg().getWidth(null),getPlayerImg().getHeight(null));
	}
}
